class Humanoid
{
  private PVector base;
  private PVector direction;
  private float rotateDirection;
  private float baseSize;
  private boolean attacking;
  private int attackCounter;
  private int walkCounter;
  private boolean upDownFlag;
  private boolean walkFlag;  
  private PImage face = loadImage("zombieFace.jpg");
  
  public Humanoid(PVector location, PVector direction, float baseSize)
  {
    base = location;
    
    rotateDirection = PVector.angleBetween(direction, new PVector(1, 0, 0));
    this.direction = direction;
    this.baseSize = baseSize;
    upDownFlag = true;
    walkFlag = true;
    attacking = false;
    attackCounter = 0;
    walkCounter= 0;
  }
  
  public void updateLocation(PVector location)
  {
   base = location; 
  }
  
  public void updateDirection(PVector direction)
  {
    this.direction = new PVector(direction.x, 0, direction.z);
    direction.normalize();
    rotateDirection = atan(direction.x/direction.z);
    if(direction.z<0){
      rotateDirection = rotateDirection+PI;
    }
  }
  
  public void draw()
  {
    noFill();
    
    //torso
    pushMatrix(); 
    translate(base.x, base.y+baseSize*1.5, base.z);
    rotateY(rotateDirection);
    fill(204, 85, 0);
    box(baseSize, baseSize, baseSize/2);
  
    //head
    pushMatrix();
    translate(0, baseSize*0.75, 0);
    scale(baseSize/4);
    Cube cube = new Cube(face, new PVector(25, 200, 75));
    cube.draw();
    beginShape();
    popMatrix();
    
    //arms
    pushMatrix();
    translate(baseSize*0.6, baseSize/5, baseSize/5);
    pushMatrix();
    //Right Arm
    if (attacking)
    {
      if (upDownFlag)
        attackCounter++;
      else
        attackCounter--;
      rotateX(-attackCounter*PI/45);
      if (attackCounter == 16 || attackCounter == 0)
        upDownFlag = !upDownFlag;
    }
    box(baseSize/5, baseSize/5, baseSize/2);
    
    //Right Forearm
    pushMatrix();
    if (attacking)
    {
      rotateX(-attackCounter*PI/90);
      translate(0, baseSize*-0.01*abs(attackCounter), baseSize*-0.002*abs(attackCounter));
    }
    translate(0, 0, baseSize/2);
    box(baseSize/5, baseSize/5, baseSize/2);
    popMatrix();
    popMatrix();
    
    //Left Arm
    translate(baseSize*-1.2, 0, 0);
    if (attacking)
    {
     rotateX(-attackCounter*PI/45);
    }
    ambient(25, 200, 75);
    box(baseSize/5, baseSize/5, baseSize/2);
    //Left Forearm
    if (attacking)
    {
      rotateX(-attackCounter*PI/90);
      translate(0, baseSize*-0.01*abs(attackCounter), baseSize*-0.002*abs(attackCounter));
    }
    translate(0, 0, baseSize/2);
    box(baseSize/5, baseSize/5, baseSize/2);
    popMatrix();
    
    //legs
    pushMatrix();
    //Left Leg
    translate(baseSize*-0.3, baseSize*-0.6, 0);
    pushMatrix();
    if (!attacking)
    {
      if (walkFlag)
        walkCounter++;
      else
        walkCounter--;
      rotateX(walkCounter*PI/90);
      if (walkCounter == 30 || walkCounter == -30)
        walkFlag = !walkFlag;
    }
    box(baseSize/5, baseSize/2, baseSize/5);
    //Left Lower Leg
    pushMatrix();
    translate(0, baseSize*-0.45, 0);
    if (!attacking)
    {
      if (walkCounter > 0)
        rotateX(walkCounter*PI/90);
      else
        rotateX(-walkCounter*PI/90);
      translate(0, 0, baseSize*-0.005*abs(walkCounter));
    }
    box(baseSize/5, baseSize/2, baseSize/5);
    popMatrix();
    popMatrix();
    //Right Leg
    if (!attacking)
    {
        rotateX(-walkCounter*PI/90);
    }
    translate(baseSize*0.65, 0, 0);
    ambient(25, 200, 75);
    box(baseSize/5, baseSize/2, baseSize/5);
    //Right Lower Leg
    translate(0, baseSize*-0.45, 0);
    if (!attacking)
    {
      if (walkCounter >0)
        rotateX(walkCounter*PI/90);
      else
        rotateX(-walkCounter*PI/90);
      translate(0, 0, baseSize*-0.005*abs(walkCounter));
    }
    box(baseSize/5, baseSize/2, baseSize/5);
    popMatrix();
    popMatrix();
  }
  
  public void isAttacking(boolean attacking)
  {
    this.attacking = attacking;
  }
  
  
}
